/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.wx.service;

import java.util.List;

import com.jeesite.modules.common.refun.StringUtil;
import com.jeesite.modules.wx.entity.WxBusinessGroup;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.wx.entity.WxBusinessGroupOrgConfig;
import com.jeesite.modules.wx.dao.WxBusinessGroupOrgConfigDao;

/**
 * 机构商家分组配置Service
 * @author wangjianxin
 * @version 2018-11-29
 */
@Service
@Transactional(readOnly=true)
public class WxBusinessGroupOrgConfigService extends CrudService<WxBusinessGroupOrgConfigDao, WxBusinessGroupOrgConfig> {
	
	/**
	 * 获取单条数据
	 * @param wxBusinessGroupOrgConfig
	 * @return
	 */
	@Override
	public WxBusinessGroupOrgConfig get(WxBusinessGroupOrgConfig wxBusinessGroupOrgConfig) {
		return super.get(wxBusinessGroupOrgConfig);
	}
	
	/**
	 * 查询分页数据
	 * @param page 分页对象
	 * @param wxBusinessGroupOrgConfig
	 * @return
	 */
	@Override
	public Page<WxBusinessGroupOrgConfig> findPage(Page<WxBusinessGroupOrgConfig> page, WxBusinessGroupOrgConfig wxBusinessGroupOrgConfig) {
		return super.findPage(page, wxBusinessGroupOrgConfig);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param wxBusinessGroupOrgConfig
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(WxBusinessGroupOrgConfig wxBusinessGroupOrgConfig) {
		super.save(wxBusinessGroupOrgConfig);
	}
	
	/**
	 * 更新状态
	 * @param wxBusinessGroupOrgConfig
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(WxBusinessGroupOrgConfig wxBusinessGroupOrgConfig) {
		super.updateStatus(wxBusinessGroupOrgConfig);
	}
	
	/**
	 * 删除数据
	 * @param wxBusinessGroupOrgConfig
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(WxBusinessGroupOrgConfig wxBusinessGroupOrgConfig) {
		super.delete(wxBusinessGroupOrgConfig);
	}

	/**
	 * 排序处理
	 * @param wxBusinessGroupOrgConfig
	 * @param wxBusinessGroup
	 */
	public void doSort(WxBusinessGroupOrgConfig wxBusinessGroupOrgConfig, WxBusinessGroup wxBusinessGroup){

		String businessIds = wxBusinessGroup.getBusinessIds();
		String[] businessIdsArray = businessIds.split(",");
		if(businessIdsArray.length < 10){
			return;
		}
		StringBuilder sortBusinessIds = new StringBuilder();

		//排序1
		String sortOne = wxBusinessGroupOrgConfig.getSortOne();
		if(StringUtil.isNotEmpty(sortOne)){
			sortBusinessIds.append(sortOne);
		}
		//排序2
		String sortTwo = wxBusinessGroupOrgConfig.getSortTwo();
		if(StringUtil.isNotEmpty(sortTwo)){
			//包含
			if(sortBusinessIds.toString().contains(sortTwo)){
				throw new RuntimeException("排序错误，商家重复");
			}
			sortBusinessIds.append(","+sortTwo);
		}
		//排序3
		String sortThree = wxBusinessGroupOrgConfig.getSortThree();
		if(StringUtil.isNotEmpty(sortThree)){
			//包含
			if(sortBusinessIds.toString().contains(sortThree)){
				throw new RuntimeException("排序错误，商家重复");
			}
			sortBusinessIds.append(","+sortThree);
		}
		//排序4
		String sortFour = wxBusinessGroupOrgConfig.getSortFour();
		if(StringUtil.isNotEmpty(sortFour)){
			//包含
			if(sortBusinessIds.toString().contains(sortFour)){
				throw new RuntimeException("排序错误，商家重复");
			}
			sortBusinessIds.append(","+sortFour);
		}
		//排序5
		String sortFive = wxBusinessGroupOrgConfig.getSortFive();
		if(StringUtil.isNotEmpty(sortFive)){
			//包含
			if(sortBusinessIds.toString().contains(sortFive)){
				throw new RuntimeException("排序错误，商家重复");
			}
			sortBusinessIds.append(","+sortFive);
		}
		//排序6
		String sortSix = wxBusinessGroupOrgConfig.getSortSix();
		if(StringUtil.isNotEmpty(sortSix)){
			//包含
			if(sortBusinessIds.toString().contains(sortSix)){
				throw new RuntimeException("排序错误，商家重复");
			}
			sortBusinessIds.append(","+sortSix);
		}
		//排序7
		String sortSeven = wxBusinessGroupOrgConfig.getSortSeven();
		if(StringUtil.isNotEmpty(sortSeven)){
			//包含
			if(sortBusinessIds.toString().contains(sortSeven)){
				throw new RuntimeException("排序错误，商家重复");
			}
			sortBusinessIds.append(","+sortSeven);
		}
		//排序8
		String sortEight = wxBusinessGroupOrgConfig.getSortEight();
		if(StringUtil.isNotEmpty(sortEight)){
			//包含
			if(sortBusinessIds.toString().contains(sortEight)){
				throw new RuntimeException("排序错误，商家重复");
			}
			sortBusinessIds.append(","+sortEight);
		}
		//排序9
		String sortNine = wxBusinessGroupOrgConfig.getSortNine();
		if(StringUtil.isNotEmpty(sortNine)){
			//包含
			if(sortBusinessIds.toString().contains(sortNine)){
				throw new RuntimeException("排序错误，商家重复");
			}
			sortBusinessIds.append(","+sortNine);
		}
		//排序10
		String sortTen = wxBusinessGroupOrgConfig.getSortTen();
		if(StringUtil.isNotEmpty(sortTen)){
			//包含
			if(sortBusinessIds.toString().contains(sortTen)){
				throw new RuntimeException("排序错误，商家重复");
			}
			sortBusinessIds.append(","+sortTen);
		}

		logger.info("获得最终的排序商家ids={}",sortBusinessIds.toString());

		if(StringUtil.isNotEmpty(sortBusinessIds.toString())){
			wxBusinessGroup.setSortBusinessIds(sortBusinessIds.toString());
		}
	}

	/**
	 * set 排序商家
	 * @param businessId
	 * @param wxBusinessGroupOrgConfig
	 */
	public void setSort(int i,String businessId,WxBusinessGroupOrgConfig wxBusinessGroupOrgConfig){

		switch (i){
			case 0:
				wxBusinessGroupOrgConfig.setSortOne(businessId);
				break;
			case 1:
				wxBusinessGroupOrgConfig.setSortTwo(businessId);
				break;
			case 2:
				wxBusinessGroupOrgConfig.setSortThree(businessId);
				break;
			case 3:
				wxBusinessGroupOrgConfig.setSortFour(businessId);
				break;
			case 4:
				wxBusinessGroupOrgConfig.setSortFive(businessId);
				break;
			case 5:
				wxBusinessGroupOrgConfig.setSortSix(businessId);
				break;
			case 6:
				wxBusinessGroupOrgConfig.setSortSeven(businessId);
				break;
			case 7:
				wxBusinessGroupOrgConfig.setSortEight(businessId);
				break;
			case 8:
				wxBusinessGroupOrgConfig.setSortNine(businessId);
				break;
			case 9:
				wxBusinessGroupOrgConfig.setSortTen(businessId);
				break;
		}


	}
	
}